// orderConfirm.js
var app = getApp();

function countdown(that) {
  var second = that.data.second;
  var minute = that.data.minute;
  if (minute <= 0 && second <= 0) {
    that.setData({
      minute: 0
    });
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    if (second <= 0) {
      that.setData({
        minute: minute - 1,
        second: 59
      });
    }
    countdown(that);
  }, 1000)
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 60,
    minute: 4,
    messages: false,
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.hint = true;
    var activiId = options.activiId;
    this.setData({
      activiId: activiId
    })
    var takemanids = "";
    for (var i = 0; i < app.custom.length; i++) {
      takemanids += app.custom[i].TakeManId + ",";
    }
    var takemanids = takemanids.trim(',');
    this.buildOrder(activiId, takemanids, options.date, options.time)
  },

  buildOrder: function (activiId, takemanids, date, time){
    app.showLoadings();
    var that = this;
    wx.request({
      url: app.serviceurl + 'CustomLine/CustomBuildOrder',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        BusPlanId: app.line.BusPlanId,
        upPointId: app.line.upPointId,
        downPointId: app.line.downPointId,
        TakeManIDs: takemanids,
        StartDate: date,
        StartTime: time,
        ActivityType: activiId == 'undefined' ? '' : activiId,
        FormId: app.formID,
        IsByQRCode: app.IsScanCOde
      },
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (!res.data.Result) {
          app.hint = false;
          that.setData({
            messages: true,
            mes: res.data.Message,
          });
          app.hideLoadings();
          return;
        }
        // 订单号
        app.customCode = res.data.OrderCode;
        that.setData({
          orderinfo: res.data,
          OrderCode: res.data.OrderCode,
          res: res,
          refund: false
        });
        wx.setStorage({
          key: "cusOrderCode",
          data: res.data.OrderCode
        })
        app.hideLoadings();
      },
      fail: function (err) {
        console.log(err)
        app.hideLoadings();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

    clearTimeout(this.data.time)

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.time);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  openToast: function () {
    wx.showToast({
      title: '支付完成',
      icon: 'success',
      duration: 600
    });
  },

  wxpay: function (e) {
    this.setData({
      disabled:true
    })
    var that = this;
    wx.request({
      url: app.serviceurl + 'CustomLine/CustomPayOrder',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: { OrderCode: that.data.OrderCode, ActivityType: this.data.activiId, },
      success: function (res) {
        if (res.data.Result) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function (resr) {
              if (resr.errMsg == "requestPayment:ok") {
                //支付成功
                app.hint = false;
                that.openToast()
                var orderState = '已付款';
                //发送一个通知给后台，告诉用户已经支付完成，让后台检测一下订单状态是否正确更改，根据情况向用户发送即时通知。
                wx.request({
                  url: app.serviceurl + 'CustomLine/NotifyForCustomLinePayFinished',
                  header: {
                    Authorization: "Bearer " + app.accesstoken
                  },
                  data: { orderSn: that.data.OrderCode, package: res.data.package },
                  success: function (res) {
                    //不需要处理
                    wx.redirectTo({
                      url: '../detail/detail?orderstatus=' + orderState + '&distance=' + app.customCode
                    })
                  }
                });
              }
            }
          })
        }
        else {
          wx.showToast({
            title: '调起支付失败',
          })
        }
      },
      fail: function (error) {
        console.log(error);
      },
      complete: function () {
        var time = setTimeout(function () {
          that.setData({
            disabled: false,
            // judge: false      
          });
        }, 1000)
      }
    });
  },

  anewChoose: function () {
    wx.navigateBack({});
  },
  phoneservice: function (e) {
    wx.makePhoneCall({
      phoneNumber: "96606"
    });
  },
})